﻿using DebIt.Common.Model;
using DebIt.Persistence.Model;

namespace DebIt.Service
{
    public static class DebitConverter
    {
        #region Client

        public static Client ConvertToModelObject(this ClientPo po)
        {
            if (po == null)
                return null;
            return new Client
                       {
                           ClientId = po.ClientId,
                           EMailAddress = po.EMailAddress,
                           LastName = po.LastName,
                           Phonenumber = po.Phonenumber,
                           FirstName = po.FirstName,
                       };
        }

        public static ClientPo ConvertToPo(this Client client)
        {
            if (client == null)
                return null;
            return new ClientPo
            {
                ClientId = client.ClientId,
                EMailAddress = client.EMailAddress,
                LastName = client.LastName,
                Phonenumber = client.Phonenumber,
                FirstName = client.FirstName,
            };
        }

        #endregion


        #region Loan

        public static Loan ConvertToModelObject(this LoanPo po)
        {
            if (po == null)
                return null;
            return new Loan
            {
                LoanId = po.LoanId,
                Name = po.Name,
                Description = po.Description,
                Currency = po.Currency,
                TimeStamp = po.Timestamp
            };
        }

        public static LoanPo ConvertToPo(this Loan loan)
        {
            if (loan == null)
                return null;
            return new LoanPo
            {
                LoanId = loan.LoanId,
                Name = loan.Name,
                Description = loan.Description,
                Timestamp = loan.TimeStamp,
                Currency = loan.Currency
            };
        }

        #endregion


        #region LoanItem

        public static LoanItem ConvertToModelObject(this LoanItemPo po)
        {
            if (po == null)
                return null;
            return new LoanItem
            {
                Description = po.Description,
                Value = po.Value,
                LoanItemId = po.LoanItemId,
                Loan = po.Loan.ConvertToModelObject()
            };
        }

        public static LoanItemPo ConvertToPo(this LoanItem loanItem)
        {
            if (loanItem == null)
                return null;
            return new LoanItemPo
            {
                Description = loanItem.Description,
                Loan = loanItem.Loan.ConvertToPo(),
                Value = loanItem.Value,
                LoanItemId = loanItem.LoanItemId
            };
        }

        #endregion


        #region ClientLoanState

        public static ClientLoanState ConvertToModelObject(this ClientLoanStatePo po)
        {
            if (po == null)
                return null;
            return new ClientLoanState
            {
                Client = po.Client.ConvertToModelObject(),
                Loan = po.Loan.ConvertToModelObject(),
                IsPaid = po.IsPaid,
                Timestamp = po.TimestampPaid,
                ClientLoanStateId = po.ClientLoanStateId
            };
        }

        public static ClientLoanStatePo ConvertToPo(this ClientLoanState clientLoanState)
        {
            if (clientLoanState == null)
                return null;
            var cl = clientLoanState.Client.ConvertToPo();
            return new ClientLoanStatePo
            {
                Client = cl,
                Loan = clientLoanState.Loan.ConvertToPo(),
                IsPaid = clientLoanState.IsPaid,
                ClientLoanStateId = clientLoanState.ClientLoanStateId,
                TimestampPaid = clientLoanState.Timestamp
            };
        }

        #endregion
    }
}
